﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ESRI.ArcGIS.esriSystem;
using System.Configuration;

namespace ClientApplication
{
    static class Program
    {

        private static LicenseInitializer m_AOLicenseInitializer = new ClientApplication.LicenseInitializer();
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
         {
            //Database连接信息
            //GISServer服务器信息
            string gisServerUsername = null;
            string gisServerPassword = null;
            string gisServerUrl = null;
            GisServer gisServer = new GisServer();
            //dbServer Infomation
            DBServer dbServer = new DBServer();
            //ESRI License Initializer generated code.
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngine, esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB, esriLicenseProductCode.esriLicenseProductCodeBasic, esriLicenseProductCode.esriLicenseProductCodeStandard, esriLicenseProductCode.esriLicenseProductCodeAdvanced },
            new esriLicenseExtensionCode[] { esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst, esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork, esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst, esriLicenseExtensionCode.esriLicenseExtensionCodeSchematics, esriLicenseExtensionCode.esriLicenseExtensionCodeMLE, esriLicenseExtensionCode.esriLicenseExtensionCodeDataInteroperability, esriLicenseExtensionCode.esriLicenseExtensionCodeTracking });
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //读取配置文件判断是否已经有arcgis server的账户信息
            //获取Configuration对象
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //根据Key读取<add>元素的Value
            //并且判断是否为空
            try
            {
                //读取GISServer Info
                gisServer.GisUrl = config.AppSettings.Settings["gisserver"].Value;

                gisServer.GisUsername = config.AppSettings.Settings["gisusername"].Value;

                gisServer.GisPassword = config.AppSettings.Settings["gispassword"].Value;

                //read dbServer info
                dbServer.DbUsername = config.AppSettings.Settings["dbusername"].Value;
                dbServer.DbPassword = config.AppSettings.Settings["dbpassword"].Value;
                dbServer.DbUrl = config.AppSettings.Settings["dbserver"].Value;
                dbServer.DbName = config.AppSettings.Settings["dbname"].Value;

                if (gisServer.IsNull() || dbServer.IsNull())
                {
                    Config myConfig = new Config();
                    myConfig.ShowDialog();
                    //gisServerUrl = config.AppSettings.Settings["server"].Value;
                    //gisServerUsername = config.AppSettings.Settings["username"].Value;
                    //gisServerPassword = config.AppSettings.Settings["password"].Value;
                    //读取GISServer Info
                    gisServer.GisUrl = config.AppSettings.Settings["gisserver"].Value;

                    gisServer.GisUsername = config.AppSettings.Settings["gisusername"].Value;

                    gisServer.GisPassword = config.AppSettings.Settings["gispassword"].Value;

                    //read dbServer info
                    dbServer.DbUsername = config.AppSettings.Settings["dbusername"].Value;
                    dbServer.DbPassword = config.AppSettings.Settings["dbpassword"].Value;
                    dbServer.DbUrl = config.AppSettings.Settings["dbserver"].Value;
                    dbServer.DbName = config.AppSettings.Settings["dbname"].Value;
                    
                }
            }
            catch (NullReferenceException e)
            {
                MessageBox.Show(e.Message);
                throw new SystemException(e.Message);
            }
            //刷新，否则程序读取的还是之前的值（可能已装入内存）
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //gisServerUrl = config.AppSettings.Settings["server"].Value;
            //gisServerUsername = config.AppSettings.Settings["username"].Value;
            //gisServerPassword = config.AppSettings.Settings["password"].Value;
            //读取GISServer Info
            gisServer.GisUrl = config.AppSettings.Settings["gisserver"].Value;

            gisServer.GisUsername = config.AppSettings.Settings["gisusername"].Value;

            gisServer.GisPassword = config.AppSettings.Settings["gispassword"].Value;

            //read dbServer info
            dbServer.DbUsername = config.AppSettings.Settings["dbusername"].Value;
            dbServer.DbPassword = config.AppSettings.Settings["dbpassword"].Value;
            dbServer.DbUrl = config.AppSettings.Settings["dbserver"].Value;
            dbServer.DbName = config.AppSettings.Settings["dbname"].Value;
            //Application.Run(new LoadMng());
            //Application.Run(new MainClient(gisServerUrl, gisServerUsername, gisServerPassword));
            Application.Run(new MainClient(gisServer, dbServer));
            //ESRI License Initializer generated code.
            //Do not make any call to ArcObjects after ShutDownApplication()
            m_AOLicenseInitializer.ShutdownApplication();
        }
    }
}